﻿ using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

using ESRI.ArcGIS.ADF.BaseClasses;
using PlanningGIS.Framework.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
//using ESRI.ArcGIS.TOCControl;
using PlanningGIS.Framework;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 作用：加载图层数据
    /// 作者：
    /// 编写日期：2009-03-25
    /// </summary>
    internal  class AddLayerData:BaseCommand 
    {
        public AddLayerData()
        {
            base.m_caption = "加载图层数据";
            base.m_toolTip = base.Caption;
           
        }
        IApplication m_Application = null;
        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }

        public override void OnClick()
        {

            if (!((m_Application as IFramework).TOCControl.CustomProperty is IMap || (m_Application as IFramework).TOCControl.CustomProperty is IGroupLayer)) return;
            try
            {
               
                OpenFileDialog ofdlg = new OpenFileDialog();
                ofdlg.Multiselect = true;
                ofdlg.RestoreDirectory = true;
                ofdlg.Title = "加载图层数据";
                ofdlg.Filter = "图层文件(*.lyr)|*.lyr";
                DialogResult dr = ofdlg.ShowDialog();
                if (DialogResult.OK == dr)
                {
                    string[] files = ofdlg.FileNames;

                    foreach (string file in files)
                    {
                        if (File.Exists(file))
                        {
                            if ((m_Application as IFramework).TOCControl.CustomProperty is IMap)
                            {
                    
                                (m_Application as IFramework).MapControl.AddLayerFromFile(file, 0);
                            }
                            else if ((m_Application as IFramework).TOCControl.CustomProperty is IGroupLayer)
                            {
                                IGroupLayer pGroupLayer = (m_Application as IFramework).TOCControl.CustomProperty as IGroupLayer;
                                (m_Application as IFramework).MapControl.AddLayerFromFile(file, 0);
                                ILayer pLayer = (m_Application as IFramework).MapControl.get_Layer(0);
                                (m_Application as IFramework).MapControl.Map.DeleteLayer((m_Application as IFramework).MapControl.get_Layer(0));
                                pGroupLayer.Add(pLayer);
                            }
                        }
                    }
                    (m_Application as IFramework).TOCControl.Update();
                }
            }
            catch (System.Exception)
            {
                MsgHelper.ShowErrorMsgbox("加载图层数据出错");
            }

        }
        public override bool Enabled
        {
            get
            {
                return (m_Application as IFramework).IsMapCtrlactive;
            }
        }
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
    
}
